//
//  DZMReadViewController.swift
//  DZMeBookRead
//
//  Created by 邓泽淼 on 2017/5/12.
//  Copyright © 2017年 DZM. All rights reserved.
//

import UIKit

class DZMReadViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    /// 章节page
    var chapterPage:CLBookRecord?
    
    /// 阅读控制器
    weak var readController:CLReadViewController!
    
    /// 顶部状态栏
    private(set) var topStatusView:UILabel!
    
    /// 底部状态栏
    private(set) var bottomStatusView:DZMRMStatusView!
    
    /// TableView
    private(set) var tableView:UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 添加子控件
        self.addSubviews()
        
        // 配置背景颜色
        if CLReadConfigure.shared.isNight {
            self.configureNightBGColor()
        } else {
            self.configureBGColor()
        }
        
        // 配置阅读模式
        self.configureReadEffect()
        
        // 配置阅读记录
        //configureReadRecordModel()
    }
    
    /// 创建UI
    func addSubviews() {
        
        // TopStatusView
        self.topStatusView = UILabel()
        self.topStatusView.text = self.chapterPage?.chapter?.name

        self.topStatusView.lineBreakMode = .byTruncatingMiddle
        self.topStatusView.textColor = UIColor.cj.color_4
        self.topStatusView.font = UIFont.system._12
        self.topStatusView.frame = CGRect(x: CGFloat.readerSpace._1, y: 0, width: self.view.width - 2 * CGFloat.readerSpace._1, height: CGFloat.readerSpace._2)
        self.view.addSubview(self.topStatusView)
        self.topStatusView.snp.makeConstraints { (make) in
            if #available(iOS 11.0, *) {
                if UIScreen.main.bounds.height != 812.0 && UIScreen.main.bounds.height != 896.0 {
                    make.top.equalToSuperview()
                } else {
                    make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
                }
            } else {
                make.top.equalToSuperview()
            }
            make.left.equalToSuperview().offset(CGFloat.readerSpace._1)
            make.right.equalToSuperview().offset(-CGFloat.readerSpace._1)
            make.height.equalTo(CGFloat.readerSpace._2)
        }
        
        // BottomStatusView
        self.bottomStatusView = DZMRMStatusView(frame:CGRect.zero)
        self.bottomStatusView.backgroundColor = UIColor.clear
        
        if let page = self.chapterPage, let chapter = page.chapter {
            self.bottomStatusView.titleLabel.text = "\(page.page + 1)/\(chapter.pages)"
        }
        
        self.view.addSubview(self.bottomStatusView)
        self.bottomStatusView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(CGFloat.readerSpace._1)
            make.right.equalToSuperview().offset(-CGFloat.readerSpace._1)
            make.height.equalTo(CGFloat.readerSpace._2)
            if #available(iOS 11.0, *) {
                make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
            } else {
                make.bottom.equalToSuperview()
            }
        }
        
        // UITableView
        self.tableView = UITableView(frame: CGRect.zero, style: .plain)
        self.tableView.backgroundColor = UIColor.clear
        self.tableView.showsVerticalScrollIndicator = false
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
        
        self.tableView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15.0)
            make.right.equalToSuperview().offset(-15.0)
            make.top.equalTo(self.topStatusView.snp.bottom)
            make.bottom.equalTo(self.bottomStatusView.snp.top)
        }
        
        // 清理
        /*
        for readChapterListModel in readController.readModel.readChapterListModels {
            readChapterListModel.clearPageCount(readRecordModel: readRecordModel)
        }
         */
    }
    
    // MARK: -- 配置背景颜色

    /// 配置背景颜色
    func configureBGColor() {
        self.view.backgroundColor = CLReadConfigure.shared.readColor
        self.bottomStatusView.setColors(textColor: CLReadConfigure.shared.readHeaderAndFooterColor)
        self.topStatusView.textColor = CLReadConfigure.shared.readHeaderAndFooterColor
    }
    
    /// 配置夜间背景颜色
    func configureNightBGColor() {
        self.view.backgroundColor = UIColor.readBg.night
        self.bottomStatusView.setColors(textColor: UIColor.readText.night)
        self.topStatusView.textColor = UIColor.readHeaderAndFooterText.night
    }
    
    // MARK: -- 阅读模式
    
    /// 配置阅读效果
    func configureReadEffect() {
        self.tableView.isScrollEnabled = false
    }
    
    // MARK: -- UITableViewDelegate,UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = DZMReadViewCell.cellWithTableView(tableView)
        if let page = self.chapterPage?.page {
            cell.frameRef = self.chapterPage?.chapter?.frame(page: page)
        }
        cell.readController = self.readController
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return DZMReadView.readFrame.size.height
    }
    
    deinit {
        // 释放该临时模型
        self.chapterPage = nil
        // 移除定时器
        self.bottomStatusView?.removeTimer()
    }
}
